<template>
	<view class="tabs">
		<scroll-view id="tab-bar" :scroll-x="true" :show-scrollbar="true" :scroll-into-view="scrollInto">
			<view style="flex-direction: column;">
				<view style="flex-direction: row;text-align: center;">
					<view class="uni-tab-item" v-for="(tab,index) in tabList" :key="tab.id" :id="tab.id"
						:ref="'tabitem'+index" :data-id="index" :data-current="index" @click="ontabtap">
						<text class="uni-tab-item-title"
							:class="tabIndex==index ? 'uni-tab-item-title-active' : ''">{{tab.name}}</text>
					</view>
				</view>
				<view class="scroll-view-indicator">
					<view ref="underline" class="scroll-view-underline" :style="{left: indicatorLineLeft + 'px', width: indicatorLineWidth + 'px'}"></view>
				</view>
			</view>
		</scroll-view>

		<swiper class="tab-view" id="tab-bar-view" :current="tabIndex" :duration="300" @transition="onswiperscroll"
			@animationfinish="animationfinish">
			<swiper-item class="swiper-item" :key="0">
				<tuiguang :productId="productId" />
			</swiper-item>
			<swiper-item class="swiper-item" :key="1">
				<list :productId="productId" />
			</swiper-item>
		</swiper>
	</view>
</template>
<script>
	// #ifdef APP-PLUS
	const dom = weex.requireModule('dom');
	// #endif
	// 缓存页签数量
	const MAX_CACHE_PAGE = 2;
	const TAB_PRELOAD_OFFSET = 1;
	import tuiguang from '@/components/tuiguang.vue'
	import list from '@/components/list.vue'

	export default {
		components: {
			tuiguang,
			list
		},
		data() {
			return {
				scrollInto: '',
				tabIndex: 0,
				cacheTab: [],
				tabList: [{
					id: 'tg',
					name: '推广'
				}, {
					id: 'cxjl',
					name: '查询记录'
				}],
				_lastTabIndex: 0,
				indicatorLineLeft: 0,
				indicatorLineWidth: 0,

				productId: null
			}
		},
		onLoad(option) {
			this.productId = option.id ? option.id : 1
		},
		onReady() {
			this._lastTabIndex = 0
			this.swiperWidth = 0
			this.tabbarWidth = 0
			this.tabListSize = {}
			this.selectorQuery()
		},
		methods: {
			ontabtap(e) {
				let index = e.target.dataset.current || e.currentTarget.dataset.current;
				// #ifdef APP-PLUS || H5 || MP-WEIXIN || MP-QQ
				var currentSize = this.tabListSize[index];
				this.updateIndicator(currentSize.left, currentSize.width);
				// #endif
				this.switchTab(index);
			},
			onswiperscroll(e) {
				var offsetX = e.detail.dx;
				var preloadIndex = this._lastTabIndex;
				if (offsetX > TAB_PRELOAD_OFFSET) {
					preloadIndex++;
				} else if (offsetX < -TAB_PRELOAD_OFFSET) {
					preloadIndex--;
				}
				if (preloadIndex === this._lastTabIndex || preloadIndex < 0) {
					return;
				}
				// #ifdef APP-PLUS || H5 || MP-WEIXIN || MP-QQ
				var percentage = Math.abs(this.swiperWidth / offsetX);
				var currentSize = this.tabListSize[this._lastTabIndex];
				var preloadSize = this.tabListSize[preloadIndex];
				if (!preloadSize) return
				var lineL = currentSize.left + (preloadSize.left - currentSize.left) / percentage;
				var lineW = currentSize.width + (preloadSize.width - currentSize.width) / percentage;
				this.updateIndicator(lineL, lineW);
				// #endif
			},
			animationfinish(e) {
				// #ifdef APP-PLUS || H5 || MP-WEIXIN || MP-QQ
				let index = e.detail.current;
				this._lastTabIndex = index;
				this.switchTab(index);
				this.updateIndicator(this.tabListSize[index].left, this.tabListSize[index].width);
				// #endif
			},
			selectorQuery() {
				// #ifdef MP-WEIXIN || H5 || MP-QQ
				uni.createSelectorQuery().in(this).select('.tab-view').fields({
					dataset: true,
					size: true,
				}, (res) => {
					this.swiperWidth = res.width;
				}).exec();
				uni.createSelectorQuery().in(this).selectAll('.uni-tab-item').boundingClientRect((rects) => {
					rects.forEach((rect) => {
						this.tabListSize[rect.dataset.id] = rect;
					})
					this.updateIndicator(this.tabListSize[this.tabIndex].left, this.tabListSize[this.tabIndex]
						.width);
				}).exec();
				// #endif
			},
			updateIndicator(left, width) {
				this.indicatorLineLeft = left;
				this.indicatorLineWidth = width;
			},
			switchTab(index) {
				if (this.tabIndex === index) {
					return;
				}
				this.tabIndex = index;
				// 释放 tabId
				if (this.cacheTab.length > MAX_CACHE_PAGE) {
					let cacheIndex = this.cacheTab[0];
					this.clearTabData(cacheIndex);
					this.cacheTab.splice(0, 1);
				}
			},
			scrollTabTo(index) {
				const el = this.$refs['tabitem' + index][0];
				let offset = 0;
				if (index > 0) {
					offset = this.tabbarWidth / 2 - this.tabListSize[index].width / 2;
					if (this.tabListSize[index].right < this.tabbarWidth / 2) {
						offset = this.tabListSize[0].width;
					}
				}
				dom.scrollToElement(el, {
					offset: -offset
				});
			}
		}
	}
</script>

<style>
	/* #ifndef APP-PLUS */
	page {
		width: 100%;
		min-height: 100%;
		display: flex;
	}

	/* #endif */
	.tabs {
		flex: 1;
		flex-direction: column;
		overflow: hidden;
		background-color: #ffffff;
	}

	.tab-bar {
		width: 750upx;
		height: 84upx;
		flex-direction: row;
		/* #ifndef APP-PLUS */
		white-space: nowrap;
		/* #endif */
	}

	/* #ifndef APP-NVUE */
	.tab-bar ::-webkit-scrollbar {
		display: none;
		width: 0 !important;
		height: 0 !important;
		-webkit-appearance: none;
		background: transparent;
	}

	/* #endif */

	.scroll-view-indicator {
		position: relative;
		height: 2px;
		background-color: transparent;
	}

	.scroll-view-underline {
		position: absolute;
		top: 0;
		bottom: 0;
		width: 0;
		background-color: #007AFF;
	}

	.uni-tab-item {
		/* #ifndef APP-PLUS */
		display: inline-block;
		/* #endif */
		width: 50%;
		flex-wrap: nowrap;
		padding-left: 25px;
		padding-right: 25px;
	}

	.uni-tab-item-title {
		color: #555;
		font-size: 30upx;
		height: 80upx;
		line-height: 80upx;
		flex-wrap: nowrap;
	}

	.uni-tab-item-title-active {
		color: #007AFF;
	}

	.tab-view {
		height: 100%;
		flex: 1;
	}

	.swiper-item {
		flex: 1;
		flex-direction: column;
		background: linear-gradient(180deg, #c4d6ff, #f7f8fa);
	}
</style>